4 * Created on July 7, 2007
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
32 * Query module to enumerate all available pages.
36 class ApiQueryAllLinks
extends ApiQueryGeneratorBase
{
38 public function __construct($query, $moduleName) {
39 parent
:: __construct($query, $moduleName, 'al');
42 public function execute() {
46 public function executeGenerator($resultPageSet) {
47 $this->run($resultPageSet);
50 private function run($resultPageSet = null) {
53 $params = $this->extractRequestParams();
54 $this->debugPrint($params);
56 $prop = array_flip($params['prop']);
57 $fld_ids = isset($prop['ids']);
58 $fld_title = isset($prop['title']);
60 if ($params['unique']) {
61 if (!is_null($resultPageSet))
62 $this->dieUsage($this->getModuleName() . ' cannot be used as a generator in unique links mode', 'params');
64 $this->dieUsage($this->getModuleName() . ' cannot return corresponding page ids in unique links mode', 'params');
65 $this->addOption('DISTINCT');
68 $this->addTables('pagelinks');
69 $this->addWhereFld('pl_namespace', $params['namespace']);
71 if (!is_null($params['from']))
72 $this->addWhere('pl_title>=' . $db->addQuotes(ApiQueryBase
:: titleToKey($params['from'])));
73 if (isset ($params['prefix']))
74 $this->addWhere("pl_title LIKE '" . $db->strencode(ApiQueryBase
:: titleToKey($params['prefix'])) . "%'");
76 if (is_null($resultPageSet)) {
77 $this->addFields(array (
81 $this->addFieldsIf('pl_from', $fld_ids);
83 $this->addFields('pl_from');
87 $this->addOption('USE INDEX', 'pl_namespace');
88 $limit = $params['limit'];
89 $this->addOption('LIMIT', $limit+
1);
90 $this->addOption('ORDER BY', 'pl_namespace, pl_title');
92 $res = $this->select(__METHOD__
);
96 while ($row = $db->fetchObject($res)) {
97 if (++
$count > $limit) {
98 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
99 // TODO: Security issue - if the user has no right to view next title, it will still be shown
100 $this->setContinueEnumParameter('from', ApiQueryBase
:: keyToTitle($row->pl_title
));
104 if (is_null($resultPageSet)) {
105 $title = Title
:: makeTitle($row->pl_namespace
, $row->pl_title
);
106 if ($title->userCanRead()) {
109 $vals['fromid'] = intval($row->pl_from
);
111 $vals['ns'] = intval($title->getNamespace());
112 $vals['title'] = $title->getPrefixedText();
117 $pageids[] = $row->pl_from
;
120 $db->freeResult($res);
122 if (is_null($resultPageSet)) {
123 $result = $this->getResult();
124 $result->setIndexedTagName($data, 'l');
125 $result->addValue('query', $this->getModuleName(), $data);
127 $resultPageSet->populateFromPageIDs($pageids);
131 protected function getAllowedParams() {
137 ApiBase
:: PARAM_ISMULTI
=> true,
138 ApiBase
:: PARAM_DFLT
=> 'title',
139 ApiBase
:: PARAM_TYPE
=> array (
144 'namespace' => array (
145 ApiBase
:: PARAM_DFLT
=> 0,
146 ApiBase
:: PARAM_TYPE
=> 'namespace'
149 ApiBase
:: PARAM_DFLT
=> 10,
150 ApiBase
:: PARAM_TYPE
=> 'limit',
151 ApiBase
:: PARAM_MIN
=> 1,
152 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
153 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
158 protected function getParamDescription() {
160 'from' => 'The page title to start enumerating from.',
161 'prefix' => 'Search for all page titles that begin with this value.',
162 'unique' => 'Only show unique links. Cannot be used with generator or prop=ids',
163 'namespace' => 'The namespace to enumerate.',
164 'limit' => 'How many total links to return.'
168 protected function getDescription() {
169 return 'Enumerate all pages sequentially in a given namespace';
172 protected function getExamples() {
174 'api.php?action=query&list=alllinks&alunique&alfrom=B',
178 public function getVersion() {
179 return __CLASS__
. ': $Id$';